package com.atguigu.edu.util;

import org.apache.flink.api.common.serialization.SimpleStringSchema;
import org.apache.flink.api.common.typeinfo.BasicTypeInfo;
import org.apache.flink.api.common.typeinfo.TypeInformation;
import org.apache.flink.streaming.connectors.kafka.FlinkKafkaConsumer;
import org.apache.flink.streaming.connectors.kafka.FlinkKafkaProducer;
import org.apache.flink.streaming.connectors.kafka.KafkaDeserializationSchema;
import org.apache.kafka.clients.consumer.ConsumerConfig;
import org.apache.kafka.clients.consumer.ConsumerRecord;
import org.apache.kafka.clients.producer.ProducerConfig;


import java.util.Properties;

import static io.debezium.relational.history.KafkaDatabaseHistory.BOOTSTRAP_SERVERS;

public class KafkaUtil {
    private static String BOOTSTRAP_SERVERS_CONFIG = "hadoop100:9092,hadoop101:9092,hadoop102:9092";

    public static FlinkKafkaConsumer<String> getKafkaConsumer(String topicName, String groupID){
        Properties properties = new Properties();
        properties.setProperty(ConsumerConfig.GROUP_ID_CONFIG,groupID);
        properties.setProperty(ConsumerConfig.BOOTSTRAP_SERVERS_CONFIG,BOOTSTRAP_SERVERS_CONFIG);

        FlinkKafkaConsumer<String> flinkKafkaConsumer = new FlinkKafkaConsumer<>(topicName, new KafkaDeserializationSchema<String>() {
            @Override
            public TypeInformation<String> getProducedType() {
                return BasicTypeInfo.STRING_TYPE_INFO;
            }

            @Override
            public boolean isEndOfStream(String nextElement) {
                return false;
            }

            @Override
            public String deserialize(ConsumerRecord<byte[], byte[]> record) throws Exception {
                if (record == null || record.value() == null) {
                    return "";
                }
                return new String(record.value());
            }
        }, properties);

        return flinkKafkaConsumer;
    }


    public static String getKafkaDDL(String topicName,String groupId) {
        return " WITH (\n" +
                "  'connector' = 'kafka',\n" +
                "  'topic' = '"  + topicName+ "',\n" +
                "  'properties.bootstrap.servers' = '" + BOOTSTRAP_SERVERS + "',\n" +
                "  'properties.group.id' = '" + groupId + "',\n" +
                "  'scan.startup.mode' = 'group-offsets',\n" +
                "  'format' = 'json'\n" +
                ")";
    }

    public static String getKafkaSinkDDL(String topicName) {
        return " WITH (\n" +
                "  'connector' = 'kafka',\n" +
                "  'topic' = '"  + topicName+ "',\n" +
                "  'properties.bootstrap.servers' = '" + BOOTSTRAP_SERVERS + "',\n" +
                "  'format' = 'json'\n" +
                ")";
    }

    public static String getUpsertKafkaDDL(String topicName) {

        return " WITH (\n" +
                "  'connector' = 'upsert-kafka',\n" +
                "  'topic' = '"  + topicName+ "',\n" +
                "  'properties.bootstrap.servers' = '" + BOOTSTRAP_SERVERS + "',\n" +
                "  'key.format' = 'json',\n" +
                "  'value.format' = 'json' " +
                ")";
    }

    public static FlinkKafkaProducer<String> getKafkaProducer(String topic){
        Properties prop = new Properties();
        prop.setProperty(ProducerConfig.BOOTSTRAP_SERVERS_CONFIG, BOOTSTRAP_SERVERS_CONFIG);
        return new FlinkKafkaProducer<String>(topic,new SimpleStringSchema(),prop);
    }


}
